from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
from .ser import *


# Create your views here.


class UserM(APIView):
    def get(self, request):
        user = User.objects.all()
        ser = UserSer(user, many=True)
        return Response({'code': 200, 'list': ser.data})


class ShopM(APIView):
    def get(self, request):
        shop = Shop.objects.all()
        ser = ShopSer(shop, many=True)
        return Response({'code': 200, 'list': ser.data})

    def delete(self, request):
        id = request.GET.get('id')
        Shop.objects.filter(id=id).delete()
        return Response({'code': 200})


class LoginM(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = User.objects.filter(username=username).first()
        if username == username:
            return Response({'code': 200, 'userid': user.id})
        else:
            return Response({'code': 20010})
